<?php

namespace App\Models\Admin;
//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 后台用户表
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

use App\Tools\AliSms;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;

class AdminUser extends Model
{

    protected $table = 'admin_user';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    /**
     * 验证后台账号密码
     * @param $userName
     * @param $userPassword
     */
    public static function isAdminUser($userName, $userPassword)
    {
        $info = self::where(['username' => $userName, 'store_id' => 0])->first();
        if ($info) {
            $mypassword = md5($userPassword . $info['safe']);
            if ($info['password'] == $mypassword) {
//            if (1) {
                AdminUser::where('id', $info['id'])->increment('login_num', 1, ['login_time' => time(), 'login_ip' => $_SERVER['REMOTE_ADDR']]);
                $userToken = [
                    'user_id' => $info['id'],
                    'user_name' => $info['nickname'] ?: $info['username'],
                    'store_id' => $info['store_id'],
                    'role_id' => $info['role_id'],
                ];
                //写入session
                session(['adminUserToken' => lockToken(json_encode($userToken, JSON_UNESCAPED_UNICODE))]);
                session()->save();
                return true;
            }
        }
        return false;
    }

    /**
     * 获取权限
     * @param $id
     * @return array|mixed
     */
    public static function getMyAuth($id)
    {
        $info = self::where(['id' => $id])->first();
        if ($info) {
            if ($info['role_id'] == 1) {
                return 'all';
            } else {
                $roleInfo = AdminRole::getInfoById($info['role_id']);
                return $roleInfo['auth'];
            }
        }
        return [];
    }

    /**
     * 添加修改信息
     * @param $dataArr
     */
    public static function userEdit($dataArr)
    {
        $info = [
            'role_id' => $dataArr['role_id'],
            'mobile' => $dataArr['mobile'],
            'username' => $dataArr['username'],
            'status' => $dataArr['status'] ?: 1
        ];
        if ($dataArr['password']) {
            $safe = pbGetRandStr();
            $info['safe'] = $safe;
            $info['password'] = md5($dataArr['password'] . $safe);
        }
        if ($dataArr['id']) {
            return self::where(['id' => $dataArr['id']])->update($info);
        } else {
            $info['created_at'] = time();
            return self::insert($info);
        }
    }

    /**
     * @param $storeId
     * @param $username
     * @param $password
     * @param $mobile
     * @return mixed
     */
    public static function storeUserAdd($storeId, $username, $password, $mobile)
    {
        $safe = pbGetRandStr();
        $mypassword = md5($password . $safe);
        $info = [
            'role_id' => 2,
            'store_id' => $storeId,
            'mobile' => $mobile,
            'username' => $username,
            'password' => $mypassword,
            'safe' => $safe,
            'status' => 1,
            'created_at' => time()
        ];
        $re = self::insert($info);
        if ($re) {
            AliSms::sendList($mobile, 5, $username, ['password' => $password]);
        }
        return $re;
    }
}